<template>
  <div id="global-header">
    <a-row :wrap="false">
      <a-col flex="200px">
        <router-link to="/">
          <div class="title-bar">
            <img class="logo" src="@/assets/logo.png" alt="logo" />
            <div class="title">云协同文档</div>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="handleMenuClick"
        />
      </a-col>
      <a-col flex="120px">
        <div class="user-login-status">
          <div v-if="userStore.loginUser.id">
            <a-dropdown>
              <a-space>
                <a-avatar :src="userStore.loginUser.avatar" />
                {{ userStore.loginUser.nickname ?? '无名' }}
              </a-space>
              <template #overlay>
                <a-menu>
                  <a-menu-item @click="handleLogout">
                    <LogoutOutlined />
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import { computed, h, ref } from 'vue'
import { HomeOutlined, LogoutOutlined } from '@ant-design/icons-vue'
import { MenuProps, message } from 'ant-design-vue'
import { useUserStore } from '@/stores/userStore.ts'
import { useRouter } from 'vue-router'
import { logoutApi } from '@/api/UserApi.ts'

const userStore = useUserStore()

const originMenus = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/admin/userManage',
    label: '用户管理',
    title: '用户管理',
  },
]

const router = useRouter()

// 路由跳转
const handleMenuClick = ({ key }) => {
  router.push({ path: key })
}
// 高亮菜单项
const current = ref<string[]>([])
router.afterEach((to, from, next) => {
  current.value = [to.path]
})

// 退出登录
const handleLogout = async () => {
  try {
    const res = await logoutApi()
    if (res.data.code === 0) {
      userStore.setLoginUser({
        id: '',
        username: '',
        nickname: '',
      })
      await router.replace('/user/login')
    } else {
      message.error(res.data.message)
    }
  } catch (e: any) {
    message.error('退出登录失败，' + e.message)
  }
}

// 菜单权限校验
const filterMenus = (menus = [] as MenuProps['items']) => {
  return menus?.filter((menu) => {
    if (menu?.key?.startsWith('/admin')) {
      const loginUser = userStore.loginUser
      if (!loginUser || loginUser.userRole !== 'admin') {
        return false
      }
    }
    return true
  })
}

// 过滤后的权限菜单
const items = computed(() => filterMenus(originMenus))

</script>

<style scoped>
#global-header {
  .title-bar {
    display: flex;
    align-items: center;

    .logo {
      height: 48px;
    }

    .title {
      color: black;
      font-size: 18px;
      margin-left: 16px;
    }
  }
}
</style>
